Cumulative Plot Sales

Column

Plots Sold Across the World

Row

Plot Locations

Plots Sold in the US

World Plots

Column

Cumulative Plot Sales

country sold
United States 1014
United Kingdom 131
France 63
Japan 40
United Arab Emirates 36
China 35
Egypt 33
Italy 33
Spain 31
Brazil 30
Germany 28
Hong Kong 21
Netherlands 21
India 19
Vatican City 19
Israel 18
Australia 15
Russia 13
Saudi Arabia 13
Mexico 12
Ukraine 12
Austria 11
Monaco 10
Canada 9
Greece 9
South Korea 9
Turkey 9
Singapore 8
Peru 7
Poland 5
Portugal 5
Belgium 4
Czech Republic 4
Iraq 4
Switzerland 4
Thailand 4
coordinates 3
Finland 3
Georgia 3
Malaysia 3
Saint Lucia 3
NA 3
Argentina 2
Armenia 2
Azerbaijan 2
Cambodia 2
Croatia 2
French Polynesia 2
Iceland 2
Jordan 2
Taiwan 2
Chile 1
Indonesia 1
Luxembourg 1
Malta 1
Nepal 1
Norway 1
Ontario 1
Senegal 1

Column

Day

Week

Month

Year

Total

US Plots

Column

Cumulative US Plot Sales

state sold
New York 232
California 153
Nevada 128
Texas 122
Florida 92
District of Columbia 39
Washington 26
Illinois 23
Massachusetts 22
New Jersey 18
Ohio 18
Indiana 15
Pennsylvania 15
Arizona 14
Louisiana 14
Missouri 12
Colorado 11
Georgia 11
Minnesota 9
Michigan 5
Tennessee 5
Kentucky 4
North Carolina 4
Virginia 4
Wisconsin 4
South Dakota 3
Utah 3
Hawaii 2
Wyoming 2
Alabama 1
Kansas 1
Maryland 1
New Mexico 1

Column

Day

Week

Month

Year

Total

---
title: "SuperWorld Plot Sales"

output: 
  flexdashboard::flex_dashboard:
    social: menu
    source_code: embed
    theme: yeti

---

Cumulative Plot Sales
=====================================

Inputs {.sidebar}
-------------------------------------

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
library(leaflet)
library(leaflet.extras)
library(sf)
library(tidyverse)
library(rnaturalearth)
library(rnaturalearthdata)
library(plotly)
library(usmap)
library(lubridate)

plots_sold = read_csv("C:/Users/rebec/SuperWorld_Plot_Recommendation/data/plots_sold.csv")[-1]
plots_sold$code = toupper(plots_sold$code)

us_plots = plots_sold[which(plots_sold$code == "US"),]
us_address = us_plots$address

state = c()
for (i in 1:length(us_address)){
  add = tail(unlist(str_split(us_address[i], pattern = ", ")), 2)[1]
  add = gsub(' [[:digit:]]+', '', add)
  state = c(state, add)
}

us_plots = cbind(us_plots, state) 

state_data = data.frame(state) %>%
  group_by(state) %>%
  summarise(sold = n())

```

*Total Plot Sales:*

```{r}
nrow(plots_sold)
```


*Top 10 Countries:* ```{r} plots_sold %>% group_by(country) %>% summarise(`plots sold` = n()) %>% arrange(-`plots sold`) %>% head(10) %>% knitr::kable() ```
*Top 10 US States:* ```{r} state_data %>% summarise(state, `plots sold` = sold) %>% arrange(-`plots sold`) %>% head(10) %>% knitr::kable() ``` Column {data-width=800} ------------------------------------- ### Plots Sold Across the World ```{r warning=FALSE, message=FALSE} world = ne_countries(scale = "medium", returnclass = "sf") df = st_sf(merge(plots_sold, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) df_plot = df %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradient(trans = "log") + geom_sf_text(aes(label = code), size = 2) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) # df2 = df %>% # group_by(country, code) %>% # summarise(sold = n()) %>% # mutate(sold = ifelse(is.na(country), 0, sold)) # plot(df2["sold"], logz = TRUE, main = NULL, key.pos = 4) p = ggplotly(df_plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=0, yshift=-1, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) p ``` Row ------------------------------------- ### Plot Locations ```{r} leaflet(plots_sold) %>% addTiles() %>% addCircles(lng = ~lon, lat = ~lat) %>% setView(lat = 37.0902, lng = -95.7129, zoom = 4) ``` ### Plots Sold in the US ```{r} us = plot_usmap(data = state_data, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_continuous(name = "Plots Sold") p_us = ggplotly(us) p_us ``` World Plots ===================================== Column {data-width=200} ------------------------------------- ### Cumulative Plot Sales ```{r} plots_sold %>% group_by(country) %>% summarize(sold = n()) %>% arrange(-sold) %>% knitr::kable() ``` Column {.tabset} ------------------------------------- ### Day ```{r} plots_today = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 1) df_today = st_sf(merge(plots_today, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_today %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradient(trans = "log") + geom_sf_text(aes(label = code), size = 2) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=0, yshift=-1, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Week ```{r} plots_week = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 7) df_week = st_sf(merge(plots_week, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_week %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradient(trans = "log") + geom_sf_text(aes(label = code), size = 2) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=0, yshift=-1, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Month ```{r} plots_month = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 30) df_month = st_sf(merge(plots_month, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_month %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradient(trans = "log") + geom_sf_text(aes(label = code), size = 2) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=0, yshift=-1, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Year ```{r} plots_year = plots_sold %>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 365) df_year = st_sf(merge(plots_year, world, by.x = "code", by.y = "iso_a2", all.x = FALSE, all.y = TRUE, returnclass = "sf")) plot = df_year %>% group_by(country, code) %>% summarise(sold = n()) %>% mutate(sold = ifelse(is.na(country), 0, sold)) %>% ggplot() + geom_sf(aes(fill = sold))+ scale_fill_gradient(trans = "log") + geom_sf_text(aes(label = code), size = 2) + theme(axis.title.x=element_blank(), axis.title.y=element_blank(), legend.title = element_text("Plots Sold")) + labs(caption = "Sold values are in log scale") + guides(fill = guide_colourbar(barwidth = 0.5, barheight = 10)) ggplotly(plot) %>% layout(annotations = list(x = 1, y = -0.1, text = "Sold values are in log scale", showarrow = F, xref='paper', yref='paper', xanchor='right', yanchor='auto', xshift=0, yshift=-1, font=list(size=15)), xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE) ) ``` ### Total ```{r} p ``` US Plots ===================================== Column {data-width=200} ------------------------------------- ### Cumulative US Plot Sales ```{r} us_plots %>% group_by(state) %>% summarize(sold = n()) %>% arrange(-sold) %>% knitr::kable() ``` Column {.tabset} ------------------------------------- ### Day ```{r} us_today = us_plots%>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 1) %>% group_by(state) %>% summarise(sold = n()) us_today = plot_usmap(data = us_today, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_continuous(name = "Plots Sold") ggplotly(us_today) ``` ### Week ```{r} us_week = us_plots%>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 7) %>% group_by(state) %>% summarise(sold = n()) us_week = plot_usmap(data = us_week, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_continuous(name = "Plots Sold") ggplotly(us_week) ``` ### Month ```{r} us_month = us_plots%>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 30) %>% group_by(state) %>% summarise(sold = n()) us_month = plot_usmap(data = us_month, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_continuous(name = "Plots Sold") ggplotly(us_month) ``` ### Year ```{r} us_year = us_plots%>% mutate(days = interval(date, today()) %/% days(1)) %>% filter(days < 365) %>% group_by(state) %>% summarise(sold = n()) us_year = plot_usmap(data = us_year, values = "sold", regions = "states") + theme(legend.position = "right") + scale_fill_continuous(name = "Plots Sold") ggplotly(us_year) ``` ### Total ```{r} p_us ```